package com.xuecheng.teaching.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.commons.enums.ErrorCode;
import com.xuecheng.commons.model.dto.CommentReplyDto;
import com.xuecheng.commons.model.vo.CommentReplyVo;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.utils.AuthInfoHolder;
import com.xuecheng.commons.utils.BeanHelper;
import com.xuecheng.system.api.SystemService;
import com.xuecheng.system.domain.User;
import com.xuecheng.teaching.domain.CommentReply;
import com.xuecheng.teaching.mappers.CommentReplyMapper;
import com.xuecheng.teaching.service.CommentReplyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.web.exceptions.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.net.httpserver.AuthFilter;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 作者
 * @since 2022-10-10
 */
@Service
public class CommentReplyServiceImpl extends ServiceImpl<CommentReplyMapper, CommentReply> implements CommentReplyService {
    @Autowired
    private SystemService systemService;

//    课程评论回复
    @Override
    public ResponseResult<CommentReplyVo> commentreply(CommentReplyDto dto) {
//        LambdaQueryWrapper<CommentReply>wrapper=new LambdaQueryWrapper<>();
//        wrapper.eq(CommentReply::getCommentId,dto.getCommentId());
//        CommentReply one = this.getOne(wrapper);

        CommentReply one=new CommentReply();
        one.setCommentId(Long.valueOf(dto.getCommentId()));
        one.setForbidReply(dto.getForbidReply());
        one.setReplyText(dto.getReplyText());
       one.setUserId(AuthInfoHolder.getUserId());
        ResponseResult<User> userData = systemService.findByid(AuthInfoHolder.getUserId());
        User user = userData.getData();
        String name = user.getName();
        one.setUserName("666");
        one.setNickName(name);
        one.setReplyDate(new Date());
        one.setStatus(1);//0隐藏1显示
        one.setPraiseNum(10086);
        boolean save = this.save(one);
        if (!save){
            throw  new BusinessException(ErrorCode.ERROR);
        }
        CommentReplyVo commentReplyVo = BeanHelper.copyProperties(one, CommentReplyVo.class);

        return ResponseResult.okResult(commentReplyVo);
    }
}
